import cache from "@/utils/cache.js";
import {cacheConstants} from "@/config/constants.js";
import request from "@/utils/request.js";

function getToken() {
	const token = cache.getCache(cacheConstants.user_token);
	if (token == null) {
		uni.reLaunch({
			url: '/pages/login/index'
		});
	}
}

function setToken(token){
	cache.setCache(cacheConstants.user_token,token);
}

function isLogin() {
	const token = cache.getCache(cacheConstants.user_token);
	if (token == '') {
		uni.reLaunch({
			url: "/pages/login/login"
		});
	}else{
		request({
			url:"getInfo"
		}).then(res=>{
			setUser(res.user);
		})
	}
}

function getUser(){
	return cache.getCache(cacheConstants.user);
}

function setUser(user){
	cache.setCache(cacheConstants.user,user);
}
function setAccount(user){
	const data={
		username:user.username,
		password:user.password
	}
	cache.setCache(cacheConstants.account,data);
}

function getAccount(){
	return cache.getCache(cacheConstants.account);
}

export default {
	getToken,
	setToken,
	setUser,
	getUser,
	isLogin,
	setAccount,
	getAccount
}